﻿using System.Linq;
using System.Text;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;

namespace NazTek.Documenter.Ssis.Control.Tasks.PipeSubComponent
{
    internal sealed class OleDbSource : Base
    {
        internal OleDbSource(SsisPipe parent, IDTSComponentMetaData100 subComponent)
            : base(subComponent, parent)
        {
        }

        protected override Operation GetOperation(SsisPipe parent)
        {
            var statement = new StringBuilder("SELECT<br>");
            var outputs = Metadata.OutputCollection[0];
            var outCols = outputs.OutputColumnCollection.Cast<IDTSOutputColumn100>().ToArray();
            foreach (var inCol in outputs.ExternalMetadataColumnCollection.Cast<IDTSExternalMetadataColumn100>()
                .Where(col => outCols.Any(m => m.ExternalMetadataColumnID == col.ID)))
                statement.AppendFormat("\t{0} -> {1}<br>", inCol.Name,
                    outCols.First(i => i.ExternalMetadataColumnID == inCol.ID).Name);
            return new Operation
            {
                ConnectionName = parent.Parent.GetConnectionName(Metadata),
                Statement = string.Format("{0}FROM {1}", statement, GetTableName(Metadata))
            };
        }
    }
}